package cn.wjdiankong.levideo.dialog;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.WindowManager;
import android.widget.PopupWindow;
import cn.wjdiankong.levideo.R;

public class VideoLongClickPopupMenu {
	
	private PopupWindow mMenu;
	private View parentView;
	private View anchorView, anchorRootView;
	
	private boolean isOneScreen = false;//区分是否为双流的列表
	private int position;
	
	@SuppressLint("InflateParams")
	public VideoLongClickPopupMenu(Activity act){
		if(parentView == null){
			LayoutInflater inflater = (LayoutInflater) act.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			parentView = inflater.inflate(R.layout.layout_longclick_video_menu, null);
		}
		if(mMenu == null){
			mMenu = new PopupWindow(parentView,
	                WindowManager.LayoutParams.WRAP_CONTENT,
	                WindowManager.LayoutParams.WRAP_CONTENT, true);
	        mMenu.setBackgroundDrawable(null);
	        mMenu.setAnimationStyle(R.style.menushow);
	        mMenu.setInputMethodMode(PopupWindow.INPUT_METHOD_NEEDED); // 解决
	        mMenu.setTouchable(true);
	        mMenu.setOutsideTouchable(true);
		}
		
        // //必须设置该项才能保证相应按键事件
		parentView.setFocusableInTouchMode(true);
        // 点击外部消失
		parentView.setOnTouchListener(new View.OnTouchListener() {
            @SuppressLint("ClickableViewAccessibility")
			@Override
            public boolean onTouch(View v, MotionEvent event) {
                if (mMenu != null && mMenu.isShowing()) {
                    mMenu.dismiss();
                }
                return true;
            }
        });
        // 避免 长安menu键盘
		parentView.setOnKeyListener(new View.OnKeyListener() {
            private long preClickTime = 0l;

            @Override
            public boolean onKey(View v, int keyCode, KeyEvent event) {
                long curClickTime = System.currentTimeMillis();
                if (keyCode == KeyEvent.KEYCODE_MENU
                        && event.getAction() == KeyEvent.ACTION_DOWN) {
                    if (preClickTime == 0
                            || (curClickTime - preClickTime) > 200) {
                        if (mMenu.isShowing()) {
                            mMenu.dismiss();
                        }
                    }
                    preClickTime = curClickTime;
                    return true;
                } else if (keyCode == KeyEvent.KEYCODE_BACK
                        && event.getAction() == KeyEvent.ACTION_DOWN) {
                    if (mMenu.isShowing()) {
                        mMenu.dismiss();
                    }
                }

                return false;
            }
        });
        mMenu.update();
        
        parentView.findViewById(R.id.delete).setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				if(mListener != null){
					mListener.clickItem(MenuClickItemListener.DELETE_VIDEO);
				}
				toggleMenu();
			}
		});
        parentView.findViewById(R.id.detail).setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				if(mListener != null){
					mListener.clickItem(MenuClickItemListener.DETAIL_VIDEO);
				}
				toggleMenu();
			}
		});
	}
	
	public void setMenuShowAnchorView(View view, View parentView, int position){
		anchorView = view;
		anchorRootView = parentView;
		this.position = position;
	}
	
	public void setOneScreen(boolean isOneScreen){
		this.isOneScreen = isOneScreen;
	}
	
	@SuppressLint("RtlHardcoded")
	public void toggleMenu(){
		int[] a = new int[2];
		anchorView.getLocationOnScreen(a);
		int height = anchorView.getHeight();
		int width = anchorView.getWidth();
        if (mMenu.isShowing()) {
            mMenu.dismiss();
        } else {
        	if(isOneScreen){
        		mMenu.showAtLocation(anchorRootView, Gravity.TOP | Gravity.CENTER, 0, a[1]+height/2);
        	}else{
        		if(position % 2 == 0){
        			//偶数行
        			mMenu.showAtLocation(anchorRootView, Gravity.TOP | Gravity.CENTER, -width/2, a[1]+height/2);
        		}else{
        			//奇数行
        		}
        		mMenu.showAtLocation(anchorRootView, Gravity.TOP | Gravity.CENTER, width/2, a[1]+height/2);
        	}
        }
    }
	
	public interface MenuClickItemListener{
		public static final int DELETE_VIDEO = 1;
		public static final int DETAIL_VIDEO = 2;
		void clickItem(int type);
	}
	
	private MenuClickItemListener mListener;
	public void setMenuClickItemListener(MenuClickItemListener listener){
		mListener = listener;
	}

}
